<?php

namespace App\Admin\Metrics\Examples;

use Dcat\Admin\Widgets\Metrics\RadialBar;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Tickets extends RadialBar
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();
        $this->title('今日实时数据');
        $this->height(400);
        $this->chartHeight(300);
        $this->chartLabels('已完成订单');
//        $this->dropdown([
//            '7' => 'Last 7 Days',
//            '28' => 'Last 28 Days',
//            '30' => 'Last Month',
//            '365' => 'Last Year',
//        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {

        switch ($request->get('option')) {
            case '365':
            case '30':
            case '28':
            case '7':
            default:

                //成交金额
                $num = DB::table('gr_order')->where('is_pay','1')->sum('total_money');
                $this->withFooter($num, 63, '29%');

                $nums = DB::table('gr_order')->where('is_pay','1')->count();
                $total = DB::table('gr_order')->count();
                // 成交量
                $this->withContent($total);
                //完成订单占比
                $this->withChart($nums/$total*100);
        }
    }

    /**
     * 设置图表数据.
     *
     * @param int $data
     *
     * @return $this
     */
    public function withChart(int $data)
    {
        return $this->chart([
            'series' => [$data],
        ]);
    }

    /**
     * 卡片内容
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex flex-column flex-wrap text-center">
    <h1 class="font-lg-2 mt-2 mb-0">{$content}</h1>
    <small>成交量</small>
</div>
HTML
        );
    }

    /**
     * 卡片底部内容.
     *
     * @param string $new
     * @param string $open
     * @param string $response
     *
     * @return $this
     */
    public function withFooter($new, $open, $response)
    {
        return $this->footer(
            <<<HTML
<div class="d-flex justify-content-between p-1" style="padding-top: 0!important;">
    <div class="text-center">
        <p>付款金额(美金)</p>
        <span class="font-lg-1">{$new}</span>
    </div>
    <div class="text-center">
        <p>活跃用户（今日）</p>
        <span class="font-lg-1">{$open}</span>
    </div>
    <div class="text-center">
        <p>转化率（%）</p>
        <span class="font-lg-1">{$response}</span>
    </div>
</div>
HTML
        );
    }
}
